package com.lbd.base.controller;

/**
 * Created by WangYajie on 2016/9/23.
 * <p>
 * 百度的web uploader 相关的处理
 *
 * @param file 上传的文件名
 * @return 访问的相对路径
 * @throws IOException
 * <p>
 * 处理ueditor相关请求
 * @param request  请求
 * @param response 响应
 * <p>
 * 百度的web uploader 相关的处理
 * @param file 上传的文件名
 * @return 访问的相对路径
 * @throws IOException
 * <p>
 * 处理ueditor相关请求
 * @param request  请求
 * @param response 响应
 *//*
@Controller
@RequestMapping("/upload")
public class UploadController extends BaseController {
    private final Logger log = Logger.getLogger(this.getClass());
    @Value("${filepath}")
    String BASE_PATH;
    @Value("${nginxUrl}")
    String NGINX_URL;

    *//**
 * 百度的web uploader 相关的处理
 *
 * @param file 上传的文件名
 * @return 访问的相对路径
 * @throws IOException
 *//*
    @RequestMapping(value = "/image", method = RequestMethod.POST)
    @ResponseBody
    public String upload(MultipartFile file) throws IOException {
        //201609/061234短uuid.jpg
        String filePath = "";
        filePath += DateFormatUtils.format(new Date(), "yyyyMM") + File.separator;
        filePath += DateFormatUtils.format(new Date(), "ddHHmm");
        filePath += ShortUUID.generateShortUuid();
        filePath += file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf('.'));
        FileUtils.writeByteArrayToFile(new File(BASE_PATH + File.separator + filePath), file.getBytes());
        //  前台需要的是url不是文件路径格式
        return filePath.replace(File.separator, "/");
    }

    *//**
 * 处理ueditor相关请求
 *
 * @param request  请求
 * @param response 响应
 *//*
    @RequestMapping(value = "/ueditorUtil")
    public void ueditorUtil(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.setCharacterEncoding("utf-8");
            response.setHeader("Content-Type", "text/html");
            Map<String, Object> context = new HashMap<>();
            context.put("basePath", BASE_PATH);
            context.put("nginxUrl", NGINX_URL);
            InputStream inputStream = UploadController.class.getClassLoader().getResourceAsStream("config.json");
            byte[] b = new byte[inputStream.available()];
            int read = inputStream.read(b);
            if (read < 0) {
                throw new ControllerException("该图片不存在");
            }
            inputStream.close();
            context.put("configContext", new String(b));
            response.getWriter().write(new ActionEnter(request, context).exec());
        } catch (Exception e) {
            log.warn("ueditor 编辑器获取资源失败", e);
        }
    }

}*/
